<template>
  <div class="login">
    <el-form :model="loginForm" :rules="loginRules" ref="loginForm" class="login-form">
      <h3 class="title">兜兜集后台管理系统</h3>
      <!-- 用户名 -->
      <el-form-item prop="username">
        <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
        >
          <svg-icon slot="prefix" icon-class="user" class="el-icon-s-custom"/>
        </el-input>
      </el-form-item>

      <!-- 密码 -->
      <el-form-item prop="password">
        <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-icon-lock"/>
        </el-input>
      </el-form-item>

      <!-- 验证码 -->
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%;"
            @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-icon-info"/>
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>

      <!-- 记住密码 -->
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0 0 25px 0;">记住密码</el-checkbox>

      <!-- 登陆 -->
      <el-form-item>
        <el-button
            type="primary"
            style="width:100%;"
            size="medium"
            @click.native="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>

      <!-- 注册 -->
      <div style="float: right;" v-if="register">
        <router-link class="link-type" :to="'/register'">立即注册</router-link>
      </div>
    </el-form>

    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2023 doudou.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import {getCodeImg} from "@/api/login";

export default {
  name: "login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: 'admin',
        password: 'admin123',
        rememberMe: true,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
        ]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  created() {
    this.getCode();
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    // 登陆
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store.dispatch('Login', this.loginForm).then((response) => {
            this.$router.push({path: this.redirect || '/admin'})
          }).catch(() => {
            if (this.captchaEnabled) {
              this.getCode();
            }
          })
        }
      })
    },
    // 获取图片验证码
    getCode() {
      getCodeImg().then(response => {
        console.log(response);
        this.captchaEnabled = response.data.captchaEnabled === undefined ? true : response.data.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + response.data.img;
          this.loginForm.uuid = response.data.uuid;
        }
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">

.login {
  background-image: url("@/assets/images/login-background.jpg");
  background-size: cover;
  position: fixed;
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.title {
  color: #707070;
  text-align: center;
  margin: 0 auto 30px;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  box-sizing: border-box;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.input-icon {
  height: 39px;
  width: 14px;
  margin-left: 2px;
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial, system-ui;
  font-size: 12px;
  letter-spacing: 1px;
}

.link-type {
  color: #337ab7;
  cursor: pointer;
  text-decoration: none;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.login-code-img {
  height: 38px;
}
</style>
